﻿#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "oled12864.h"
#include <stdlib.h>
#include "stmflash.h"
 #include "adc.h"
 
/************************************************
Stm32F103C8t6工程模板
************************************************/

int get_key_val()
{
	if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2)==0)
	{
		return 2;
	}
	if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1)==0)
	{
		return 1;
	}
	return 0;
}

void key_init(void)
{
	GPIO_InitTypeDef  GPIO_InitStructure;
 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);	 //使能A端口时钟
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2;	 
 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 		 //推挽输出
 	GPIO_Init(GPIOA, &GPIO_InitStructure);	  //初始化PA0,1
}

int main(void)
{	
	unsigned char key_num = 0;
	unsigned char cactus_category = 0;
	unsigned char cactus_length = 8;
	unsigned int score = 0;
	unsigned int highest_score = 0;
	int height = 0;
	int cactus_pos = 128;
	unsigned char cur_speed = 30;
	char failed = 0;
	char reset = 0;

	delay_init();	    //延时函数初始化	
	key_init();
	uart_init(115200);
	Adc_Init();
	//Flash_Write(FLASH_USER_START_ADDR, 0);
	highest_score = Flash_Read(FLASH_USER_START_ADDR);
//	printf("%d\r\n",highest_score);
  OLED_Init(); 	
	OLED_ShowString(0,0,"Test",4);
	OLED_DrawCover();
	while(get_key_val()!=2);
	delay_ms(100);
	OLED_Clear();
	
	uint8_t write_score=1;
	double v = 0;
	uint16_t bfb=0;
	uint8_t t=1;
	float add_v=4.0;
	while(1)
	{
		/* USER CODE END WHILE */
		v = Get_V(3);
		add_v += v;
		t++;
		if(t==10){
		  t=1;
			add_v = add_v/10.0f;
			bfb = (int)(((add_v-3.7)/0.5f)*100);
		}
		//bfb = Get_Adc(3);
		if (failed == 1)
		{
			if((score > highest_score)&&write_score==1) Flash_Write(FLASH_USER_START_ADDR, score);
			write_score=0;
			OLED_DrawRestart();
			key_num = get_key_val();
			if (key_num == 2)
			{
				if(score > highest_score) highest_score = score;
				score = 0;
				failed = 0;
				height = 0;
				reset = 1;
				OLED_DrawDinoJump(reset);
				OLED_DrawCactusRandom(cactus_category, reset);
				OLED_Clear();
				write_score=1;
			}
			continue;
		}

		score ++;
		if (height <= 0) key_num = get_key_val();

		OLED_DrawGround();
		OLED_DrawCloud();

		if (height>0 || key_num == 1) height = OLED_DrawDinoJump(reset);
		else OLED_DrawDino();

		cactus_pos = OLED_DrawCactusRandom(cactus_category, reset);
		if(cactus_category == 0) cactus_length = 8;
		else if(cactus_category == 1) cactus_length = 16;
		else cactus_length = 24;

		if (cactus_pos + cactus_length < 0)
		{
		  cactus_category = rand()%4;
			OLED_DrawCactusRandom(cactus_category, 1);
		}

		if ((height < 16) && ( (cactus_pos>=16 && cactus_pos <=32) || (cactus_pos + cactus_length>=16 && cactus_pos + cactus_length <=32)))
		{
			failed = 1;
		}

		OLED_ShowString(35, 0, "HI:", 12);
		OLED_ShowNum(58, 0, highest_score, 5, 12);
		OLED_ShowNum(98, 0, score, 5, 12);
		OLED_ShowNum(0,0,bfb,3,12);
		OLED_ShowString(18,0,"%",12);

		reset = 0;

//		cur_speed = score/20;
//		if (cur_speed > 29) cur_speed = 29;
//		delay_ms(30 - cur_speed);
		delay_ms(50);
//		HAL_Delay(500);
		key_num = 0;
    /* USER CODE BEGIN 3 */
	}
}


